package com.xuecheng.media.service.impl;

import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import static com.xuecheng.media.utils.MinIOFileOperationUtils.MEDIA_PROCESS_FAIL;
import static com.xuecheng.media.utils.MinIOFileOperationUtils.MEDIA_PROCESS_SUCCESS;

/**
 * @author Tang
 * @version 1.0
 * @date 2024/1/4 23:12
 */
@Slf4j
@Service
public class MediaProcessServiceImpl implements MediaProcessService {
    private final MediaProcessMapper mediaProcessMapper;
    private final MediaFilesMapper mediaFilesMapper;
    @Autowired
    private final MediaProcessHistoryMapper mediaProcessHistoryMapper;
    public MediaProcessServiceImpl(MediaProcessMapper mediaProcessMapper,
                                   MediaFilesMapper mediaFilesMapper,
                                   MediaProcessHistoryMapper mediaProcessHistoryMapper) {
        this.mediaProcessMapper = mediaProcessMapper;
        this.mediaFilesMapper = mediaFilesMapper;
        this.mediaProcessHistoryMapper = mediaProcessHistoryMapper;
    }

    @Override
    public List<MediaProcess> selectListByShardIndex(int shardTotal, int shardIndex, int count) {
        return mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
    }

    @Override
    public boolean startTask(long id) {
        // <= 0 ? false : true
        return mediaProcessMapper.startTask(id) > 0;
    }

    @Override
    @Transactional
    public void updateProcessFinishStatus(long taskId,
                                          String status,
                                          String fileId,
                                          String url,
                                          String errorMsg) {
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        if (Objects.isNull(mediaProcess)) {
            return;
        }
        // 如果任务执行失败
        if (MEDIA_PROCESS_FAIL.equals(status)) {
            mediaProcess.setStatus(MEDIA_PROCESS_FAIL);
            mediaProcess.setFailCount(mediaProcess.getFailCount() + 1);
            mediaProcess.setErrormsg(errorMsg);
            mediaProcessMapper.updateById(mediaProcess);
            return;
        }
        // 任务更新成功，需要更新media_file中的url
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
        mediaFiles.setUrl(url);
        mediaFilesMapper.updateById(mediaFiles);
        // 同时需要更新media_process表的状态
        mediaProcess.setStatus(MEDIA_PROCESS_SUCCESS);
        mediaProcess.setFinishDate(LocalDateTime.now());
        mediaProcess.setUrl(url);
        mediaProcessMapper.updateById(mediaProcess);
        // 删除当前任务表写入media_process_history中
        MediaProcessHistory history = new MediaProcessHistory();
        BeanUtils.copyProperties(mediaProcess, history);
        history.setId(null);
        mediaProcessHistoryMapper.insert(history);
        mediaProcessMapper.deleteById(mediaProcess);
        mediaProcess = null;
    }
}
